//
//  HuiDanBackCell.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2021/4/16.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "HuiDanBackCell.h"
#import "LJCustomTextView.h"
#import "XLPhotoBrowser.h"

@interface HuiDanBackCell ()<XLPhotoBrowserDelegate>

@property (nonatomic, strong) UIView *bg1View;
@property (nonatomic, strong) UILabel *title1Lab;
@property (nonatomic, strong) UIButton *btn1;
@property (nonatomic, strong) UIButton *btn2;
@property (nonatomic, strong) UIButton *btn3;

@property (nonatomic, strong) UIView *bg2View;
@property (nonatomic, strong) UITextField *companyField;
@property (nonatomic, strong) UITextField *numberField;
@property (nonatomic, strong) UILabel *imgTitleLab;
@property (nonatomic, strong) UIView *imgsView;
@property (nonatomic, strong) UILabel *line1;
@property (nonatomic, strong) UILabel *line2;
@property (nonatomic, strong) UILabel *fileTitleLab;
@property (nonatomic, strong) UILabel *fileCountLab;
@property (nonatomic, strong) LJCustomTextView *fileTextView;
@property (nonatomic, strong) UIButton *sureBtn;

@end

@implementation HuiDanBackCell

+ (instancetype)HuiDanBackCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"HuiDanBackCell";
    HuiDanBackCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[HuiDanBackCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bg1View = [UIView new];
        self.bg1View.layer.masksToBounds = YES;
        self.bg1View.layer.cornerRadius = 6;
        self.bg1View.backgroundColor = HexColor(0xffffff);
        [self.contentView addSubview:self.bg1View];
        
        self.title1Lab = [UILabel new];
        self.title1Lab.textColor = HexColor(0x2B2C44);
        self.title1Lab.font = [UIFont systemFontOfSize:16];
        self.title1Lab.text = @"回单方式";
        [self.bg1View addSubview:self.title1Lab];
        
        self.btn1 = [UIButton new];
        [self.btn1 setTitle:@"快递" forState:0];
        self.btn1.backgroundColor = HexColor(0xF2F2F5);
        [self.btn1 setTitleColor:HexColor(0xCCCCD9) forState:0];
        self.btn1.titleLabel.font = [UIFont systemFontOfSize:14];
        self.btn1.layer.masksToBounds = YES;
        self.btn1.layer.cornerRadius = 15;
        self.btn1.tag = 1;
        [self.btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.bg1View addSubview:self.btn1];

        self.btn2 = [UIButton new];
        [self.btn2 setTitle:@"附件" forState:0];
        self.btn2.backgroundColor = HexColor(0xF2F2F5);
        [self.btn2 setTitleColor:HexColor(0xCCCCD9) forState:0];
        self.btn2.titleLabel.font = [UIFont systemFontOfSize:14];
        self.btn2.layer.masksToBounds = YES;
        self.btn2.layer.cornerRadius = 15;
        self.btn2.tag = 2;
        [self.btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.bg1View addSubview:self.btn2];

        self.btn3 = [UIButton new];
        [self.btn3 setTitle:@"其他" forState:0];
        self.btn3.backgroundColor = HexColor(0xF2F2F5);
        [self.btn3 setTitleColor:HexColor(0xCCCCD9) forState:0];
        self.btn3.titleLabel.font = [UIFont systemFontOfSize:14];
        self.btn3.layer.masksToBounds = YES;
        self.btn3.layer.cornerRadius = 15;
        self.btn3.tag = 3;
        [self.btn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.bg1View addSubview:self.btn3];
        
        self.bg2View = [UIView new];
        self.bg2View.layer.masksToBounds = YES;
        self.bg2View.layer.cornerRadius = 6;
        self.bg2View.backgroundColor = HexColor(0xffffff);
        [self.contentView addSubview:self.bg2View];
        
        self.companyField = [UITextField new];
        self.companyField.textColor = HexColor(0x2B2C44);
        self.companyField.font = [UIFont systemFontOfSize:16];
        self.companyField.placeholder = @"请输入";
        [self.companyField addTarget:self action:@selector(companyChanged:) forControlEvents:UIControlEventEditingChanged];
        UIView *companyLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 90, 60)];
        UILabel *companyLeftLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 21.5, 70, 17)];
        companyLeftLab.text = @"快递公司";
        companyLeftLab.textColor = HexColor(0x2B2C44);
        companyLeftLab.font = [UIFont systemFontOfSize:16];
        [companyLeftView addSubview:companyLeftLab];
        self.companyField.leftView = companyLeftView;
        self.companyField.leftViewMode = UITextFieldViewModeAlways;
        [self.bg2View addSubview:self.companyField];
        
        self.line1 = [UILabel new];
        self.line1.backgroundColor = HexColor(0xF2F2F5);
        [self.bg2View addSubview:self.line1];
        
        self.numberField = [UITextField new];
        self.numberField.textColor = HexColor(0x2B2C44);
        self.numberField.font = [UIFont systemFontOfSize:16];
        self.numberField.placeholder = @"请输入";
        [self.numberField addTarget:self action:@selector(numberChanged:) forControlEvents:UIControlEventEditingChanged];
        UIView *numberLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 90, 60)];
        UILabel *numberLeftLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 21.5, 70, 17)];
        numberLeftLab.text = @"快递单号";
        numberLeftLab.textColor = HexColor(0x2B2C44);
        numberLeftLab.font = [UIFont systemFontOfSize:16];
        [numberLeftView addSubview:numberLeftLab];
        self.numberField.leftView = numberLeftView;
        self.numberField.leftViewMode = UITextFieldViewModeAlways;
        [self.bg2View addSubview:self.numberField];
        
        self.imgTitleLab = [UILabel new];
        self.imgTitleLab.text = @"上传附件";
        self.imgTitleLab.textColor = HexColor(0x2B2C44);
        self.imgTitleLab.font = BoldFontOfSize(18);
        [self.bg2View addSubview:self.imgTitleLab];
        
        self.imgsView = [UIView new];
        [self.bg2View addSubview:self.imgsView];
        
        self.line2 = [UILabel new];
        self.line2.backgroundColor = HexColor(0xF2F2F5);
        [self.bg2View addSubview:self.line2];
        
        self.fileTitleLab = [UILabel new];
        self.fileTitleLab.textColor = HexColor(0x2B2C44);
        self.fileTitleLab.font = [UIFont systemFontOfSize:14];
        self.fileTitleLab.text = @"备注";
        [self.bg2View addSubview:self.fileTitleLab];
        
        self.fileCountLab = [UILabel new];
        self.fileCountLab.textColor = HexColor(0xBDBED9);
        self.fileCountLab.font = [UIFont systemFontOfSize:12];
        self.fileCountLab.text = @"0/80";
        [self.bg2View addSubview:self.fileCountLab];
        
        self.fileTextView = [LJCustomTextView new];
        self.fileTextView.placeholder = @"请输入";
        self.fileTextView.textColor = HexColor(0x2B2C44);
        self.fileTextView.font = [UIFont systemFontOfSize:14];
        self.fileTextView.textLength = 80;
        __weak_self;
        self.fileTextView.countBlock = ^(NSString * _Nonnull str) {
            weakself.fileCountLab.text = str;
            if (weakself.remarkBlock) {
                weakself.remarkBlock(weakself.fileTextView.text);
            }
        };
        [self.bg2View addSubview:self.fileTextView];
        
        self.sureBtn = [UIButton new];
        self.sureBtn.layer.masksToBounds = YES;
        self.sureBtn.layer.cornerRadius = 6;
        self.sureBtn.backgroundColor = Color_ThemeColor;
        [self.sureBtn setTitle:@"确认返回" forState:0];
        [self.sureBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.sureBtn.titleLabel.font = BoldFontOfSize(16);
        [self.sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.sureBtn];
    }
    return self;
}

- (void)sureClick {
    if (self.sureBlock) {
        self.sureBlock();
    }
}

- (void)companyChanged:(UITextField *)field {
    if (self.companyBlock) {
        self.companyBlock(field.text);
    }
}

- (void)numberChanged:(UITextField *)field {
    if (self.numberBlock) {
        self.numberBlock(field.text);
    }
}

- (void)btnClick:(UIButton *)sender {
    if (self.changeBlock) {
        self.changeBlock(sender.tag);
    }
}

-(void)setModel:(HuiDanBackModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.bg1View.nim_left = 15;
    self.bg1View.nim_top = 10;
    self.bg1View.nim_width = ScreenW - 30;
    self.bg1View.nim_height = 60;
    
    [self.title1Lab sizeToFit];
    self.title1Lab.nim_left = 15;
    self.title1Lab.nim_centerY = self.bg1View.nim_height * 0.5;
    
    CGFloat btnW = 60;
    CGFloat btnH = 30;
    self.btn3.frame = CGRectMake(self.bg1View.nim_width - 15 - btnW, 15, btnW, btnH);
    self.btn2.frame = CGRectMake(self.btn3.nim_left - 20 - btnW, 15, btnW, btnH);
    self.btn1.frame = CGRectMake(self.btn2.nim_left - 20 - btnW, 15, btnW, btnH);
    
    if (self.model.method == 1) {
        self.btn1.backgroundColor = Color_ThemeColor;
        self.btn2.backgroundColor = HexColor(0xF2F2F5);
        self.btn3.backgroundColor = HexColor(0xF2F2F5);
        [self.btn1 setTitleColor:HexColor(0xffffff) forState:0];
        [self.btn2 setTitleColor:HexColor(0xCCCCD9) forState:0];
        [self.btn3 setTitleColor:HexColor(0xCCCCD9) forState:0];
    }else if (self.model.method == 2) {
        self.btn1.backgroundColor = HexColor(0xF2F2F5);
        self.btn2.backgroundColor = Color_ThemeColor;
        self.btn3.backgroundColor = HexColor(0xF2F2F5);
        [self.btn1 setTitleColor:HexColor(0xCCCCD9) forState:0];
        [self.btn2 setTitleColor:HexColor(0xffffff) forState:0];
        [self.btn3 setTitleColor:HexColor(0xCCCCD9) forState:0];
    }else if (self.model.method == 3) {
        self.btn1.backgroundColor = HexColor(0xF2F2F5);
        self.btn2.backgroundColor = HexColor(0xF2F2F5);
        self.btn3.backgroundColor = Color_ThemeColor;
        [self.btn1 setTitleColor:HexColor(0xCCCCD9) forState:0];
        [self.btn2 setTitleColor:HexColor(0xCCCCD9) forState:0];
        [self.btn3 setTitleColor:HexColor(0xffffff) forState:0];
    }
    
    self.bg2View.nim_left = self.bg1View.nim_left;
    self.bg2View.nim_top = self.bg1View.nim_bottom + 10;
    self.bg2View.nim_width = self.bg1View.nim_width;
    
    CGFloat remarkTop = 0;
    if (self.model.method == 1) {
        //快递
        self.imgTitleLab.frame = CGRectZero;
        self.imgsView.frame = CGRectZero;
        self.imgTitleLab.hidden = YES;
        self.imgsView.hidden = YES;
        self.line1.frame = CGRectZero;
        self.line2.frame = CGRectZero;
        
        self.companyField.hidden = NO;
        self.numberField.hidden = NO;
        
        self.companyField.frame = CGRectMake(15, 0, self.bg2View.nim_width - 30, 60);
        self.line1.frame = CGRectMake(15, self.companyField.nim_bottom, self.bg2View.nim_width - 15, 1);
        self.numberField.frame = CGRectMake(15, self.line1.nim_bottom, self.bg2View.nim_width - 30, 60);
        self.line2.frame = CGRectMake(15, self.numberField.nim_bottom, self.bg2View.nim_width - 15, 1);
        remarkTop = self.line2.nim_bottom;
    }else if (self.model.method == 2) {
        //附件
        self.companyField.hidden = YES;
        self.numberField.hidden = YES;
        self.companyField.frame = CGRectZero;
        self.numberField.frame = CGRectZero;
        self.line1.frame = CGRectZero;
        self.line2.frame = CGRectZero;
        
        self.imgTitleLab.hidden = NO;
        self.imgsView.hidden = NO;
        
        [self.imgTitleLab sizeToFit];
        self.imgTitleLab.nim_left = 15;
        self.imgTitleLab.nim_top = 20;
        
        self.imgsView.frame = CGRectMake(15, 60, self.bg2View.nim_width - 30, 90);
        CGFloat imgW = (self.imgsView.nim_width - 40) * 0.33333;
        self.imgsView.nim_height = imgW + 10;
        __block CGFloat viewLeft = 0;
        [self.imgsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.model.attachment enumerateObjectsUsingBlock:^(HuiDanBackImgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton *btn = [UIButton new];
            btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
            btn.nim_left = (imgW + 20) * idx;
            btn.nim_top = 10;
            btn.nim_width = imgW;
            btn.nim_height = imgW;
            [btn sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",obj.url,obj.path]] forState:0];
            btn.tag = idx;
            [btn addTarget:self action:@selector(imgPreView:) forControlEvents:UIControlEventTouchUpInside];
            [self.imgsView addSubview:btn];
            
            UIButton *deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(btn.nim_right - 15, btn.nim_top - 7, 22, 22)];
            [deleteBtn setImage:[UIImage imageNamed:@"icon_caozuodan_delete_img"] forState:0];
            deleteBtn.tag = idx;
            [deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.imgsView addSubview:deleteBtn];
            
            viewLeft = btn.nim_right + 20;
        }];
        
        if (self.model.attachment.count < 3) {
            UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(viewLeft, 10, imgW, imgW)];
            [addBtn setBackgroundImage:[UIImage imageNamed:@"icon_caozuodan_upload_img"] forState:0];
            [addBtn addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
            [self.imgsView addSubview:addBtn];
        }
        
        self.line2.frame = CGRectMake(15, self.imgsView.nim_bottom + 10, self.bg2View.nim_width - 30, 1);
        remarkTop = self.line2.nim_bottom;
    }else {
        //其他
        self.companyField.frame = CGRectZero;
        self.numberField.frame = CGRectZero;
        self.imgTitleLab.frame = CGRectZero;
        self.imgsView.frame = CGRectZero;
        self.line1.frame = CGRectZero;
        self.line2.frame = CGRectZero;
        
        self.imgTitleLab.hidden = YES;
        self.imgsView.hidden = YES;
        self.companyField.hidden = YES;
        self.numberField.hidden = YES;
    }
    
    self.fileTitleLab.nim_left = 15;
    [self.fileTitleLab sizeToFit];
    self.fileTitleLab.nim_top = remarkTop + 20;
    
    self.fileCountLab.nim_left = self.fileTitleLab.nim_right + 10;
    [self.fileCountLab sizeToFit];
    self.fileCountLab.nim_bottom = self.fileTitleLab.nim_bottom;
    self.fileCountLab.nim_width += 50;
    
    self.fileTextView.frame = CGRectMake(self.fileTitleLab.nim_left, self.fileTitleLab.nim_bottom + 10, self.bg2View.nim_width - 2 * self.fileTitleLab.nim_left, 80);
    
    self.fileTextView.customText = self.model.remarks.length?self.model.remarks:@"";
    
    self.bg2View.nim_height = self.fileTextView.nim_bottom + 15;
    
    self.sureBtn.nim_left = self.bg1View.nim_left;
    self.sureBtn.nim_top = self.bg2View.nim_bottom + 20;
    self.sureBtn.nim_width = self.bg1View.nim_width;
    self.sureBtn.nim_height = 44;
    
    self.height = self.sureBtn.nim_bottom + 10;
}

- (void)imgPreView:(UIButton *)sender {
    NSMutableArray *mut = [NSMutableArray array];
    [self.model.attachment enumerateObjectsUsingBlock:^(HuiDanBackImgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mut addObject:[NSString stringWithFormat:@"%@%@",obj.url,obj.path]];
    }];
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithImages:mut currentImageIndex:sender.tag];
    browser.browserStyle = XLPhotoBrowserStyleIndexLabel;//微博样式
    [browser setActionSheetWithTitle:nil delegate:self cancelButtonTitle:nil deleteButtonTitle:nil otherButtonTitles:@"保存图片", nil];
}

- (void)photoBrowser:(XLPhotoBrowser *)browser clickActionSheetIndex:(NSInteger)actionSheetindex currentImageIndex:(NSInteger)currentImageIndex {
    [browser saveCurrentShowImage];
}

- (void)deleteClick:(UIButton *)sender {
    if (self.deleteBlock) {
        self.deleteBlock(sender.tag);
    }
}

- (void)addClick {
    if (self.addBlock) {
        self.addBlock();
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
